import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/count_down_timer.dart';
import 'package:cbec_app/pages/my/controller/invited_friends_ctr.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../utils/Tools.dart';

/// 拼团 - 邀请好友
class InvitedFriends extends GetView<InvitedFriendsCtr> {
  const InvitedFriends({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<InvitedFriendsCtr>(builder: (controller) {
      return BasePage(
        title: "拼团详情",
        iconType: IconType.back,
        // bodyColor: Colors.white,
        children: controller.model == null
            ? [const SizedBox()]
            : [
                // 商品样式
                goodStyle(),
                const SizedBox(height: 10),
                Container(
                  padding: const EdgeInsets.only(top: 40, bottom: 20),
                  decoration: const BoxDecoration(
                    color: Colors.white,
                  ),
                  child: Column(
                    children: [
                      bookInfoStyle(),
                    ],
                  ),
                ),
              ],
      );
    });
  }

  // 商品样式
  Widget goodStyle() {
    return Container(
      padding: const EdgeInsets.all(10),
      color: Colors.white,
      width: double.infinity,
      child: Stack(
        children: [
          Row(
            children: [
              ClipRRect(
                borderRadius: const BorderRadius.all(Radius.circular(10)),
                child: Image.network(
                  controller.model!.img ?? '',
                  width: 100,
                  height: 100,
                  fit: BoxFit.cover,
                ),
              ),
              const SizedBox(
                width: 10,
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  commodityTypeTag(),
                  const SizedBox(height: 10),
                  Text(
                    controller.model?.goodsName ?? '',
                    style: TextStyle(
                      color: Tools.fromHex('#333333'),
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                  const SizedBox(height: 10),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text(
                        '¥${controller.model?.priceBook.toString()}到手价',
                        style: TextStyle(
                          color: Tools.fromHex('#F83F2D'),
                          fontSize: 12,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                      const SizedBox(width: 10),
                      Text(
                        '¥${controller.model?.price.toString()}',
                        style: TextStyle(
                          color: Tools.fromHex('#9F9F9F'),
                          fontSize: 12,
                          fontWeight: FontWeight.w400,
                          decoration: TextDecoration.lineThrough, // 添加删除线
                          decorationColor: Tools.fromHex('#9F9F9F'), // 设置删除线颜色
                          decorationThickness: 2, // 删除线的厚度
                        ),
                      ),
                    ],
                  ),
                ],
              )
            ],
          ),
        ],
      ),
    );
  }

  // 标签
  Widget commodityTypeTag() {
    return controller.model?.commodityType == 0
        ? goodsTypeTag("一般贸易")
        : controller.model?.commodityType == 1
            ? goodsTypeTag("保税")
            : controller.model?.commodityType == 2
                ? goodsTypeTag("海外直邮")
                : emptyWidget();
  }

  // 参与评团的人头像
  Widget avatarListWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start, // 根据需要设置对齐方式
      children: controller.model!.imgs!.map((img) {
        return Padding(
          padding: const EdgeInsets.only(right: 8.0), // 图片间距
          child: ClipOval(
            child: Image.network(
              img,
              width: 50,
              height: 50,
              fit: BoxFit.cover,
            ),
          ),
        );
      }).toList(),
    );
  }

  Widget bookInfoStyle() {
    var bookStatus = controller.model!.bookStatus;
    // 拼团状态，0:拼团中;1:拼团成功;2:拼团失败
    if (bookStatus == null) {
      return const SizedBox();
    } else {
      switch (bookStatus) {
        case 0:
          return Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    '还差',
                    style: TextStyle(
                      color: Tools.fromHex('#333333'),
                      fontSize: 18,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  Text(
                    controller.model!.needPeople.toString(),
                    style: const TextStyle(
                      color: ThemeConfig.themeColor,
                      fontSize: 18,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  Text(
                    '人，赶快邀请好友来拼单吧',
                    style: TextStyle(
                      color: Tools.fromHex('#333333'),
                      fontSize: 18,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 10),
              CountDownTimer(
                timeStamp: DateTime.parse(controller.model!.ld ?? '').millisecondsSinceEpoch,
                timeStyle: TimeStyle.hhmmss,
                style: setTextStyle(16, FontWeight.w400, ThemeConfig.themeColor),
                //取消订单自动返回
                callback: () {
                  Get.back();
                },
              ),
              const SizedBox(height: 10),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ClipOval(
                    child: Image.network(
                      controller.model!.avatar ?? '',
                      width: 50,
                      height: 50,
                      fit: BoxFit.cover,
                    ),
                  ),
                  if (controller.model!.imgs != null && controller.model!.imgs!.isNotEmpty) ...[
                    avatarListWidget(),
                  ] else ...[
                    ClipOval(
                      child: Image.asset(
                        'assets/images/book_avatar.png',
                        width: 50,
                        height: 50,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ],
                ],
              ),
              const SizedBox(height: 20),
              solidButton(
                margin: EdgeInsets.only(bottom: 10.w),
                "邀请好友拼单",
                textColor: Tools.fromHex("#FFFFFF"),
                borderColor: Tools.fromHex("#F7F7F7"),
                () {
                  controller.shareMiniProgram();
                },
              ),
            ],
          );
        case 1:
          return Column(
            children: [
              Text(
                '恭喜你拼团成功!',
                style: TextStyle(
                  color: Tools.fromHex('#333333'),
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                ),
              ),
              const SizedBox(height: 10),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ClipOval(
                    child: Image.network(
                      controller.model!.avatar ?? '',
                      width: 50,
                      height: 50,
                      fit: BoxFit.cover,
                    ),
                  ),
                  if (controller.model!.imgs != null && controller.model!.imgs!.isNotEmpty) ...[
                    avatarListWidget(),
                  ] else ...[
                    ClipOval(
                      child: Image.asset(
                        'assets/images/book_avatar.png',
                        width: 50,
                        height: 50,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ],
                ],
              ),
              const SizedBox(height: 20),
              solidButton(
                margin: EdgeInsets.only(bottom: 10.w),
                "查看订单",
                textColor: Tools.fromHex("#FFFFFF"),
                borderColor: Tools.fromHex("#F7F7F7"),
                () {
                  Get.toNamed(Routes.pendingPayment, arguments: controller.model?.orderId);
                },
              ),
              const SizedBox(height: 10),
              solidButton(
                margin: EdgeInsets.only(bottom: 10.w),
                "回到首页",
                textColor: Tools.fromHex("#333333"),
                backgroundColor: Colors.white,
                borderColor: Tools.fromHex("#F7F7F7"),
                () {
                  Get.until((route) => Get.currentRoute == Routes.main);
                },
              ),
            ],
          );
        case 2:
          return Column(
            children: [
              Text(
                '很遗憾，拼团失败!',
                style: TextStyle(
                  color: Tools.fromHex('#333333'),
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                ),
              ),
              const SizedBox(height: 10),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ClipOval(
                    child: Image.network(
                      controller.model?.avatar ?? '',
                      width: 50,
                      height: 50,
                      fit: BoxFit.cover,
                    ),
                  ),
                  /*
                  if (controller.model.imgs != null && controller.model.imgs!.isNotEmpty) ...[
                    avatarListWidget(),
                  ] else ...[
                    ClipOval(
                      child: Image.asset(
                        'assets/images/book_avatar.png',
                        width: 50,
                        height: 50,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ],
                  */
                ],
              ),
              const SizedBox(height: 20),
              solidButton(
                margin: EdgeInsets.only(bottom: 10.w),
                "回到首页",
                textColor: Tools.fromHex("#333333"),
                backgroundColor: Colors.white,
                borderColor: Tools.fromHex("#F7F7F7"),
                () {
                  Get.until((route) => Get.currentRoute == Routes.main);
                },
              ),
            ],
          );
        default:
          return const SizedBox();
      }
    }
  }
}
